<?php
namespace app\admin\controller;


/**
 * @title 管理员账号管理
 * @description 接口说明
 * @group admin
 * @header name:authorization require:1 default: desc:登陆的token
 */
class Admin extends Base
{
    protected $_order = 'id desc';

    /**
     * @title get获取数据
     * @description 接口说明
     * @author hardy
     * @url /admin/Admin
     * @method GET
     *
     * @param name:id type:int require:0 default:1 other: desc:唯一ID，获取某条记录详情，则为必填，id和page选填其一
     * @param name:page type:int require:0 default:1 other: desc:页码数 获取第二页以上的数据必填，默认值为1，id和page选填其一
     * @param name:size type:int require:0 default:20 other: desc:每一页的数量，默认20
     *
     * @return username:用户名
     * @return nickname:昵称
     * @return telephone:手机号
     * @return email:邮箱
     * 
     * @return item:访问权限
     */
    public function gets()
    {
        parent::gets();

    }
    /**
     * @title post创建
     * @description 用户登录
     * @author hardy
     * @url /admin/Admin
     * @method POST
     *
     *
     * @param name:id type:int require:1 default: other: desc:id
     * @param name:username type:string require:1 default: other: desc:用户名
     * @param name:password type:string require: default: other: desc:密码,不填默认值为：admin123
     * @param name:telephone type:string require: default: other: desc:手机号
     * @param name:email type:string require: default: other: desc:邮箱
     * @param name:authority type:array require: default: other: desc:访问权限
     *
     * @return username:用户名
     * @return nickname:昵称
     * @return telephone:手机号
     * @return email:邮箱
     * 
     * @return authority:访问权限
     */
    public function create(){
        parent::create();
    }
    /**
     * @title put修改
     * @description 修改密码
     * @author hardy
     * @url /admin/Admin
     * @method put
     *
     *
     * @param name:id type:int require: default: other: desc:不填则表示使用登陆者的id
     * @param name:username type:string require: default: other: desc:用户名
     * @param name:password type:string require: default: other: desc:密码，密码创空值，表示重置，重置为:admin123
     * @param name:telephone type:string require: default: other: desc:手机号
     * @param name:email type:string require: default: other: desc:邮箱
     * @param name:authority type:array require: default: other: desc:访问权限
     *
     * @return username:用户名
     * @return nickname:昵称
     * @return telephone:手机号
     * @return email:邮箱
     * 
     * @return authority:访问权限
     */
    public function update(){
        $data = input('put.');
        if (empty($data['id'])){
            $data['id'] = $this->getUserAttr('id');
        }
        $validate = Validate($this->controller);
        if (!$validate->checkRuleByData($data)) {
            $this->responseFail($validate->getError());
        }
        /**
         * 除去登陆时间字段
         */
        if (array_key_exists('login_time', $data)) {
            unset($data['login_time']);
        }
        $model = model($this->controller);
        $r = $model->updateSave($data);
        if ($r == false){
            $this->responseFail($model->getError());
        }
        sysLogs('修改管理员信息', json_encode($data));
        $this->gets();
    }
    /**
     * @title delete删除
     * @description 修改密码
     * @author hardy
     * @url /admin/Admin
     * @method delete
     *
     * @param name:id type:int require:1 default: other: desc:id
     */
    public function delete(){
        $id = input('delete.id');
        if (empty($id)){
            $this->responseFail('id必填');
        }
        if (!is_numeric($id)){
            $this->responseFail('请输入数字');
        }
        if ($id == 1) {
            $this->responseFail('无法删除初始超级管理员账号');
        }
        $model = model($this->controller);
        $r = $model->deleteData($id);
        if ($r == false){
            $this->responseFail($model->getError());
        }
        sysLogs('删除管理员', '删除id：'.$id);
        $map = [];
        $this->_searchPage($map);
        $model = model($this->controller);
        $count = $model->getListForCount($map);
        $per_page = input('get.size', 20);
        $page = input('get.page', 1);
        $data_list = $model->getListForPage($map, $page, $per_page, $this->_order);
        if (empty($data_list) && $page != 1){
            $_GET['page'] = $page - 1;
            $data_list = $model->getListForPage($map, $page-1, $per_page, $this->_order);
        }
        $this->responseSuccess($data_list, '成功删除', $count, $per_page);
    }
}